package com.mark.web.data.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.mark.common.Constant;
import com.mark.common.ServerResponse;
import com.mark.web.data.mapper.*;
import com.mark.web.data.enums.OrderStatusEnum;
import com.mark.web.data.enums.PaymentTypeEnum;
import com.mark.web.data.enums.ProductStatusEnum;
import com.mark.common.exception.Campuso2oException;
import com.mark.web.data.exception.OrderException;
import com.mark.web.data.entity.po.*;
import com.mark.web.data.service.IOrderService;
import com.mark.common.utils.BigDecimalUtil;
import com.mark.common.utils.DateTimeUtil;
import com.mark.common.utils.PropertiesUtil;
import com.mark.web.data.entity.vo.OrderItemVo;
import com.mark.web.data.entity.vo.OrderProductVo;
import com.mark.web.data.entity.vo.OrderVo;
import com.mark.web.data.entity.vo.ShippingVo;
import com.mark.web.sys.entity.po.User;
import com.mark.web.sys.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

import static com.mark.common.enums.PropertiesEnum.FTP_SERVICE_HTTP_PREFIX;

/**
 * @Description: 订单模块业务逻辑实现
 * @Author: Mark
 * @CreateDate: 2018/8/14.
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service("orderServiceImpl")
@Slf4j
public class OrderServiceImpl implements IOrderService {
    private static final String  IMAGES_HOST_URL;
    static {
        IMAGES_HOST_URL = PropertiesUtil.getProperty(FTP_SERVICE_HTTP_PREFIX.getPropertiesName());
    }
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ShippingMapper shippingMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @SuppressWarnings("unchecked")
    @Transactional(value = "mySysDataSourceTransactionManager", rollbackFor = Campuso2oException.class)
    public ServerResponse createOrder(Integer currentUserId, Integer shippingId) {
        try {
            //订单号列表
            List<Long> orderNumberList = Lists.newArrayList();
            //该订单对应的卖家id列表
            List<Integer> sellerIds = cartMapper.selectSellerIds(currentUserId);

            for (int sellerId : sellerIds) {
                List<Cart> cartList = cartMapper.selectCheckedCartByUserId(currentUserId, sellerId);
                //构建订单明细
                ServerResponse response = this.getCartOrderItem(currentUserId, cartList);
                if (!response.isSuccess()) {
                    return response;
                }
                List<OrderItem> orderItemList = (List<OrderItem>) response.getData();
                //计算总价
                BigDecimal payment = this.getOrderTotalPrice(orderItemList);
                //构建订单实例
                Order order = this.assembleOrder(currentUserId, shippingId, payment, sellerId);
                //订单子明细添加OrderNo属性,状态属性
                for (OrderItem orderItem : orderItemList) {
                    orderItem.setOrderNo(order.getOrderNo());
                    orderItem.setEnableStatus(order.getEnableStatus());
                }
                int effectNum = orderItemMapper.batchInsert(orderItemList);
                if (effectNum != orderItemList.size()) {
                    throw new Campuso2oException("批量保存订单子明细异常");
                }
                //生成订单成功,减少产品的库存
                this.reduceProductStock(orderItemList);
                //清空一下购物车
                this.cleanCart(cartList);
                //封装返回给前端的Vo
                OrderVo orderVo = assembleOrderVo(order, orderItemList);
                orderNumberList.add(orderVo.getOrderNo());
            }
            return ServerResponse.createBySuccess("新建订单成功！！", orderNumberList);
        } catch (Exception e) {
            throw new Campuso2oException("新增订单", e);
        }
    }

    @SuppressWarnings("AlibabaCommentsMustBeJavadocFormat")
    @Transactional
    @Override
    public ServerResponse cancelOrder(Integer currentUserId, Long orderNo) {
        try {
            Order order = orderMapper.selectOrder(currentUserId, orderNo, null);
            if (order == null) {
                return ServerResponse.createByErrorMessage("改用户订单不存在");
            }
            if (order.getEnableStatus() != OrderStatusEnum.NO_PAY.getCode()) {
                return ServerResponse.createByErrorMessage("取消清单失败：因为订单已经" + Constant.OrderStatusEnum.codeOf(order.getEnableStatus()).getValue());
            }
            Order updateOrder = new Order();
            updateOrder.setId(order.getId());
            updateOrder.setEnableStatus(OrderStatusEnum.CANCELED.getCode());
            updateOrder.setCloseTime(new Date()); //设置关闭时间
            int rowCount = orderMapper.updateByPrimaryKeySelective(updateOrder);
            if (rowCount > 0) {
                return ServerResponse.createBySuccessMessage("取消订单成功");
            }
            return ServerResponse.createByErrorMessage("取消订单失败");
        } catch (Exception e) {
            throw new OrderException("cancelOrder", e.getClass() + e.getMessage());
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public ServerResponse getOrderCartProduct(Integer currentUserId) {
        List<Cart> cartList = cartMapper.selectCheckedCartByUserId(currentUserId, null);
        ServerResponse response = this.getCartOrderItem(currentUserId, cartList);
        if (!response.isSuccess()) {
            return response;
        }
        List<OrderItem> orderItemList = (List<OrderItem>) response.getData();
        List<OrderItemVo> orderItemVoList = Lists.newArrayList();
        BigDecimal payment = new BigDecimal("0");
        //订单子明细总价
        for (OrderItem orderItem : orderItemList) {
            //计算总价
            payment = BigDecimalUtil.add(payment.doubleValue(), orderItem.getTotalPrice().doubleValue());
            //构建订单子明细VO对象
            OrderItemVo orderItemVo = assembleOrderItemVo(orderItem);
            orderItemVoList.add(orderItemVo);
        }
        OrderProductVo orderProductVo = new OrderProductVo();
        orderProductVo.setProductTotalPrice(payment);
        orderProductVo.setOrderItemVoList(orderItemVoList);
        orderProductVo.setImageHost(PropertiesUtil.getProperty(FTP_SERVICE_HTTP_PREFIX.getPropertiesName()));
        return ServerResponse.createBySuccess(orderProductVo);
    }

    //4.订单详情
    @Override
    public ServerResponse getOrderDetail(Integer currentUserId, Long orderNo, Integer sellerId) {
        try {
            Order order = orderMapper.selectOrder(currentUserId, orderNo, sellerId);
            if (order == null) {
                return ServerResponse.createByErrorMessage("该用户订单不存在");
            }
            List<OrderItem> orderItemList = orderItemMapper.getByOrderNoUserId(orderNo, currentUserId, sellerId);
            OrderVo orderVo = assembleOrderVo(order, orderItemList);
            return ServerResponse.createBySuccess(orderVo);
        } catch (Exception e) {
            throw new OrderException("getOrderDetail", e.getClass() + e.getMessage());
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public ServerResponse getOrderList(Integer currentUserId, int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Order> orderList = orderMapper.selectByUserId(currentUserId);
            List<OrderVo> orderVoList = assembleOrderVoList(orderList, currentUserId);
            PageInfo pageResult = new PageInfo(orderList);
            pageResult.setList(orderVoList);
            return ServerResponse.createBySuccess(pageResult);
        } catch (Exception e) {
            throw new OrderException("getOrderList", e.getClass() + e.getMessage());
        }
    }

    //admin
    @Override
    public ServerResponse<PageInfo> manageList(int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Order> orderList = orderMapper.selectAllOrder();
            List<OrderVo> orderVoList = this.assembleOrderVoList(orderList, null);
            PageInfo pageResult = new PageInfo(orderList);
            pageResult.setList(orderVoList);
            return ServerResponse.createBySuccess(pageResult);
        } catch (Exception e) {
            throw new OrderException("manageList", e.getClass() + e.getMessage());
        }
    }

    @Override
    public ServerResponse<OrderVo> manageDetail(Long orderNo) {
        try {
            Order order = orderMapper.selectByOrderNo(orderNo);
            if (order != null) {
                List<OrderItem> orderItemList = orderItemMapper.getByOrderNo(orderNo);
                OrderVo orderVo = assembleOrderVo(order, orderItemList);
                return ServerResponse.createBySuccess(orderVo);
            }
            return ServerResponse.createByErrorMessage("订单不存在");
        } catch (Exception e) {
            throw new OrderException("manageDetail", e.getClass() + e.getMessage());
        }
    }

    @Override
    public ServerResponse manageSearch(Long orderNo, int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            Order order = orderMapper.selectByOrderNo(orderNo);
            if (order != null) {
                List<OrderItem> orderItemList = orderItemMapper.getByOrderNo(orderNo);
                OrderVo orderVo = assembleOrderVo(order, orderItemList);
                List<Order> orderList = Lists.newArrayList();
                orderList.add(order);
                List<OrderVo> orderVoList = Lists.newArrayList();
                orderVoList.add(orderVo);
                PageInfo pageResult = new PageInfo(orderList);
                pageResult.setList(orderVoList);
                return ServerResponse.createBySuccess(pageResult);
            }
            return ServerResponse.createByErrorMessage("订单不存在");
        } catch (Exception e) {
            throw new OrderException("manageSearch", e.getClass() + e.getMessage());
        }
    }

    //发货
    @Override
    public ServerResponse<String> manageSendGoods(Long orderNo) {
        try {
            Order order = orderMapper.selectByOrderNo(orderNo);
            if (order != null) {
                if (order.getEnableStatus() == OrderStatusEnum.PAID.getCode()) {//订单转态为已支付
                    order.setEnableStatus(OrderStatusEnum.SHIPPED.getCode());
                    order.setSendTime(new Date());
                    orderMapper.updateByPrimaryKeySelective(order);
                    return ServerResponse.createBySuccessMessage("发货成功");
                } else {
                    return ServerResponse.createByErrorMessage("发货失败：以为该订单的状态为" + Constant.OrderStatusEnum.codeOf(order.getEnableStatus()).getValue());
                }
            }
            return ServerResponse.createByErrorMessage("订单不存在");
        } catch (Exception e) {
            throw new OrderException("manageSendGoods", e.getClass() + e.getMessage());
        }
    }

    @Override
    public ServerResponse getSellerOrderList(Integer sellerId, int pageNum, int pageSize) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Order> orderList = orderMapper.selectSellerOrders(sellerId);
            List<OrderVo> orderVoList = this.assembleOrderVoList(orderList, null);
            PageInfo pageResult = new PageInfo(orderList);
            pageResult.setList(orderVoList);
            return ServerResponse.createBySuccess(pageResult);
        } catch (Exception e) {
            throw new OrderException("getSellerOrderList", e.getClass() + e.getMessage());
        }
    }

    @Override
    public ServerResponse<String> sellerSendGoods(Long orderNo, Integer sellerId) {
        try {
            Order order = orderMapper.selectByOrderNo(orderNo);
            if (order != null) {
                if (order.getEnableStatus() == OrderStatusEnum.PAID.getCode()) {//订单转态为已支付
                    order.setEnableStatus(OrderStatusEnum.SHIPPED.getCode());
                    order.setSendTime(new Date());
                    orderMapper.updateByPrimaryKeySelective(order);
                    return ServerResponse.createBySuccess("发货成功");
                } else {
                    return ServerResponse.createByErrorMessage("发货失败：因为该订单的状态为" + Constant.OrderStatusEnum.codeOf(order.getEnableStatus()).getValue());
                }
            }
            return ServerResponse.createByErrorMessage("订单不存在");
        } catch (Exception e) {
            throw new OrderException("sellerSendGoods", e.getClass() + e.getMessage());
        }
    }

    //用户确认收货
    @Override
    public ServerResponse getGoods(Integer currentUserId, Long orderNo) {
        try {
            Order order = orderMapper.selectOrder(currentUserId, orderNo, null);
            if (order == null) {
                return ServerResponse.createByErrorMessage("该用户订单不存在");
            }
            //判断订单的状态是否为发货的状态
            if (order.getEnableStatus() != OrderStatusEnum.SHIPPED.getCode()) {
                return ServerResponse.createByErrorMessage("确认收货失败：因为订单已经" + Constant.OrderStatusEnum.codeOf(order.getEnableStatus()).getValue());
            }
            Order updateOrder = new Order();
            updateOrder.setId(order.getId());
            updateOrder.setEnableStatus(OrderStatusEnum.ORDER_SUCCESS.getCode());
            updateOrder.setEndTime(new Date()); //设置完成时间
            int rowCount = orderMapper.updateByPrimaryKeySelective(updateOrder);
            if (rowCount > 0) {
                return ServerResponse.createBySuccess("谢谢惠顾！");
            }
            return ServerResponse.createByErrorMessage("订单确认收货失败！！！");
        } catch (Exception e) {
            throw new OrderException("getGoods", e.getClass() + e.getMessage());
        }
    }

    @Override
    public ServerResponse<PageInfo> sellerSearchOrder(Long orderNo, int pageNum, int pageSize, Integer sellerId) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            Order order = orderMapper.selectByOrderNoAndSellerId(sellerId, orderNo);
            if (order != null) {
                List<OrderItem> orderItemList = orderItemMapper.getByOrderNo(orderNo);
                OrderVo orderVo = assembleOrderVo(order, orderItemList);
                List<Order> orderList = Lists.newArrayList();
                orderList.add(order);
                List<OrderVo> orderVoList = Lists.newArrayList();
                orderVoList.add(orderVo);
                PageInfo pageResult = new PageInfo(orderList);
                pageResult.setList(orderVoList);
                return ServerResponse.createBySuccess(pageResult);
            }
            return ServerResponse.createByErrorMessage("订单不存在");
        } catch (Exception e) {
            throw new OrderException("sellerSearchOrder", e.getClass() + e.getMessage());
        }
    }

    @Override
    public ServerResponse<PageInfo> searchOrderPayed(int pageNum, int pageSize, Integer sellerId) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Order> orderList = orderMapper.selectSellerOrdersPayed(sellerId, OrderStatusEnum.PAID.getCode());
            List<OrderVo> orderVoList = this.assembleOrderVoList(orderList, null);
            PageInfo pageResult = new PageInfo(orderList);
            pageResult.setList(orderVoList);
            return ServerResponse.createBySuccess(pageResult);
        } catch (Exception e) {
            throw new OrderException("searchOrderPayed", e.getClass() + e.getMessage());
        }
    }

    private ShippingVo assembleShippingVo(Shipping shipping) {
        try {
            ShippingVo shippingVo = new ShippingVo();
            shippingVo.setReceiverName(shipping.getReceiverName());
            shippingVo.setReceiverAddress(shipping.getReceiverAddress());
            shippingVo.setReceiverProvince(shipping.getReceiverProvince());
            shippingVo.setReceiverCity(shipping.getReceiverCity());
            shippingVo.setReceiverDistrict(shipping.getReceiverDistrict());
            shippingVo.setReceiverZip(shipping.getReceiverZip());
            shippingVo.setReceiverPhone(shipping.getReceiverPhone());
            return shippingVo;
        } catch (Exception e) {
            throw new OrderException("assembleShippingVo", e.getClass() + e.getMessage());
        }
    }

    //下单完成后清空购物车中相应的商品
    private void cleanCart(List<Cart> cartList) {
        for (Cart cart : cartList) {
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
    }

    /**
     * 下单完成后减少库存量
     *
     * @param orderItemList:
     */
    private void reduceProductStock(List<OrderItem> orderItemList) {
        for (OrderItem orderItem : orderItemList) {
            Product product = productMapper.selectByPrimaryKey(orderItem.getProductId());
            Integer newProductStock = product.getProductStock() - orderItem.getQuantity();
            product.setProductStock(newProductStock);
            int resultRow = productMapper.updateByPrimaryKeySelective(product);
        }
    }


    /**** 私有方法 *******************************************************************************/
    /**
     * 根据购物车中勾选的商品构建确认订单的选项
     *
     * @param currentUserId：当前的用户id
     * @param cartList:购物车列表
     * @return ：
     */
    private ServerResponse getCartOrderItem(Integer currentUserId, List<Cart> cartList) {
        List<OrderItem> orderItemList = Lists.newArrayList();
        for (Cart car : cartList) {
            Product product = productMapper.selectByPrimaryKey(car.getProductId());
            //检验商品的转态是否在售
            if (ProductStatusEnum.ON_SALE.getStatus() != product.getEnableStatus()) {
                return ServerResponse.createByErrorMessage("商品" + product.getProductName() + "不是在售状态！");
            }
            //检查库存量是否大于等于购买量
            if (car.getQuantity() > product.getProductStock()) {
                return ServerResponse.createByErrorMessage("您选择的商品" + product.getProductName() + "的库存量不足！");
            }
            //构建orderItem实例
            OrderItem orderItem = new OrderItem();
            orderItem.setUserId(currentUserId);
            orderItem.setProductId(product.getId());
            orderItem.setSellerId(product.getOwnerId());
            orderItem.setProductName(product.getProductName());
            orderItem.setProductImg(product.getMainImgAddr());
            //如果有现价
            if (product.getPromotionPrice() != null) {
                orderItem.setCurrentUnitPrice(product.getPromotionPrice());
            } else {
                orderItem.setCurrentUnitPrice(product.getNormalPrice());
            }
            orderItem.setQuantity(car.getQuantity());
            orderItem.setTotalPrice(BigDecimalUtil.mul(orderItem.getCurrentUnitPrice().doubleValue(), car.getQuantity()));
            orderItemList.add(orderItem);
        }
        return ServerResponse.createBySuccess(orderItemList);
    }

    private OrderItemVo assembleOrderItemVo(OrderItem orderItem) {
        OrderItemVo orderItemVo = new OrderItemVo();
        orderItemVo.setId(orderItem.getId());
        orderItemVo.setOrderNo(orderItem.getOrderNo());
        orderItemVo.setProductId(orderItem.getProductId());
        orderItemVo.setSellerId(orderItem.getSellerId());
        orderItemVo.setProductName(orderItem.getProductName());
        orderItemVo.setProductImage(IMAGES_HOST_URL+orderItem.getProductImg());
        orderItemVo.setCurrentUnitPrice(orderItem.getCurrentUnitPrice());
        orderItemVo.setQuantity(orderItem.getQuantity());
        orderItemVo.setTotalPrice(orderItem.getTotalPrice());
        orderItemVo.setEnableStatus(orderItem.getEnableStatus());
        if (orderItem.getEnableStatus() != null) {
            orderItemVo.setEnableStatusDes(OrderStatusEnum.codeOf(orderItem.getEnableStatus()).getInformation());
            orderItemVo.setOperation(assembleOperation(orderItem.getEnableStatus()));
        }
        orderItemVo.setCreateTime(DateTimeUtil.dateToStr(orderItem.getCreateTime()));
        orderItemVo.setPaymentTime(DateTimeUtil.dateToStr(orderItem.getPaymentTime()));
        orderItemVo.setSendTime(DateTimeUtil.dateToStr(orderItem.getSendTime()));
        orderItemVo.setEndTime(DateTimeUtil.dateToStr(orderItem.getEndTime()));
        User seller = userMapper.selectByPrimaryKey(orderItem.getSellerId());
        if (seller != null) {
            orderItemVo.setSellerName(seller.getUsername());
        }
        return orderItemVo;
    }

    /**
     * 组装订单的状态
     *
     * @param enableStatus:订单状态
     * @return ：
     */
    private String assembleOperation(Integer enableStatus) {
        switch (enableStatus) {
            case 0:
                return "已取消";
            case 10:
                return "去取消";
            case 20:
                return "等待发货";
            case 40:
                return "确认收货";
            case 50:
                return "去评论";
            case 60:
                return "去评论";
            default:
                return "不明确";
        }
    }

    private Order assembleOrder(Integer currentUserId, Integer shippingId, BigDecimal payment, Integer sellerId) {
        Order order = new Order();
        //生成订单号
        long orderNo = this.generateOrderNo();
        order.setUserId(currentUserId);
        order.setShippingId(shippingId);
        order.setSellerId(sellerId);
        order.setOrderNo(orderNo);
        order.setPayment(payment);
        //运费
        order.setPostage(0);
        //状态(未支付)
        order.setEnableStatus(OrderStatusEnum.NO_PAY.getCode());
        order.setPaymentType(Constant.PaymentTypeEnum.ONLINE_PAY.getCode());
        int rowCount = orderMapper.insert(order);
        if (rowCount > 0) {
            return order;
        } else {
            throw new Campuso2oException("保存订单失败");
        }
    }

    /**
     * 自动生成订单号
     *
     * @return ：
     */
    private synchronized long generateOrderNo() {
        long currentTime = System.currentTimeMillis();
        return currentTime + new Random().nextInt(100);
    }

    /**
     * 计算订单总价
     *
     * @param orderItemList :
     * @return :
     */
    private BigDecimal getOrderTotalPrice(List<OrderItem> orderItemList) {
        BigDecimal payment = new BigDecimal("0");
        for (OrderItem orderItem : orderItemList) {
            payment = BigDecimalUtil.add(payment.doubleValue(), orderItem.getCurrentUnitPrice().doubleValue());
        }
        return payment;
    }

    /**
     * 根据order订单列表构建orderVo列表信息
     *
     * @param orderList：订单列表
     * @param currentUserId：当前的用户id
     * @return ：
     */
    private List<OrderVo> assembleOrderVoList(List<Order> orderList, Integer currentUserId) {
        try {
            List<OrderVo> orderVoList = Lists.newArrayList();
            for (Order order : orderList) {
                List<OrderItem> orderItemList;
                //管理员
                if (currentUserId == null) {
                    orderItemList = orderItemMapper.getByOrderNo(order.getOrderNo());
                } else {
                    orderItemList = orderItemMapper.getByOrderNoUserId(order.getOrderNo(), currentUserId, null);
                }
                OrderVo orderVo = assembleOrderVo(order, orderItemList);
                orderVoList.add(orderVo);
            }
            return orderVoList;
        } catch (Exception e) {
            throw new OrderException("assembleOrderVoList", e.getClass() + e.getMessage());
        }
    }

    /**
     * 封装返回前端的VO
     *
     * @param order：订单实例
     * @param orderItemList：订单实例对应的订单子明细列表
     * @return ：orderVo对象
     */
    private OrderVo assembleOrderVo(Order order, List<OrderItem> orderItemList) {
        try {
            OrderVo orderVo = new OrderVo();
            orderVo.setShippingId(order.getShippingId());
            orderVo.setOrderNo(order.getOrderNo());
            orderVo.setPayment(order.getPayment());
            orderVo.setPostage(order.getPostage());
            orderVo.setPaymentType(order.getPaymentType());
            orderVo.setPaymentTypeDesc(PaymentTypeEnum.CodeOf(order.getPaymentType()).getInformation());
            orderVo.setStatus(order.getEnableStatus());
            orderVo.setStatusDesc(OrderStatusEnum.codeOf(order.getEnableStatus()).getInformation());

            Shipping shipping = shippingMapper.selectByPrimaryKey(order.getShippingId());
            if (shipping != null) {
                orderVo.setReceiverName(shipping.getReceiverName());
                ShippingVo shippingVo = this.assembleShippingVo(shipping);
                orderVo.setShippingVo(shippingVo);
            }
            orderVo.setPaymentTime(DateTimeUtil.dateToStr(order.getPaymentTime()));
            orderVo.setSendTime(DateTimeUtil.dateToStr(order.getSendTime()));
            orderVo.setEndTime(DateTimeUtil.dateToStr(order.getEndTime()));
            orderVo.setCreateTime(DateTimeUtil.dateToStr(order.getCreateTime()));
            orderVo.setCloseTime(DateTimeUtil.dateToStr(order.getCloseTime()));
            orderVo.setImageHost(IMAGES_HOST_URL);

            List<OrderItemVo> orderItemVoList = Lists.newArrayList();
            for (OrderItem orderItem : orderItemList) {
                OrderItemVo orderItemVo = assembleOrderItemVo(orderItem);
                orderItemVoList.add(orderItemVo);
            }
            orderVo.setOrderItemVoList(orderItemVoList);
            return orderVo;
        } catch (Exception e) {
            throw new OrderException("assembleOrderVo", e.getClass() + e.getMessage());
        }
    }
}
